package com.yungu.swift.assets.passenger.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.yungu.swift.assets.passenger.dao.PassengerMessageMapper;
import com.yungu.swift.assets.passenger.model.dto.PassengerMessageDto;
import com.yungu.swift.assets.passenger.model.param.ApiPassengerMsgParam;
import com.yungu.swift.assets.passenger.service.PassengerMessageService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.service.impl.BaseServiceImpl;
import com.yungu.swift.common.model.dto.CommonMessageStatusDto;
import com.yungu.swift.common.service.CommonMessageStatusService;
import com.yungu.swift.constants.CommonConstant;
import com.yungu.swift.system.sys.service.SysCityService;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;
import java.util.stream.Collectors;


/**
 * CommonPassengerMessageServiceImpl
 **/

@Service
public class PassengerMessageServiceImpl extends BaseServiceImpl<PassengerMessageDto> implements PassengerMessageService {

    @Autowired
    private PassengerMessageMapper passengerMessageMapper;
    @Reference
    private CommonMessageStatusService commonMessageStatusService;
    @Reference
    private SysCityService sysCityService;

    private static final String HTTP = "http://";
    private static final String HTTPS = "https://";

    @Override
    protected IMapper<PassengerMessageDto> getMapper() {
        return passengerMessageMapper;
    }


    @Override
    public ResponseData<List<PassengerMessageDto>> queryPasMsgList(ApiPassengerMsgParam msgParam) {
        Map<String, Object> paramMap = MapUtils.build(8);
        paramMap.put("appid", msgParam.getAppid());
        paramMap.put("areaID", msgParam.getAreaID());
        //截止到当前时间
        paramMap.put("sendTime", new Date());
        //乘客
        paramMap.put("targetType", CommonConstant.PASSENGER);
        paramMap.put("targetUuid", msgParam.getUserUuid());
        //消息类型：系统消息
        paramMap.put("type", CommonMessageStatusDto.TYPE_SYSTEM);
        ResponseData<String> getCityUuidByAdCode = sysCityService.getCityUuidByAdCode(msgParam.getAreaID());
        if (getCityUuidByAdCode.isSuccess() && getCityUuidByAdCode.getData() != null) {
            paramMap.put("cityUuid", getCityUuidByAdCode.getData());
        }
        //获取已发送的信息ids
        final Set<String> existsList = new HashSet<>(commonMessageStatusService.getMsgUuids(MapUtils.build("targetUuid",
                msgParam.getUserUuid())).getData());
        List<String> pasMsguuids = passengerMessageMapper.getUuids(paramMap);
        List<CommonMessageStatusDto> messageStatusDtoList = pasMsguuids.stream().filter(uuid -> !existsList.contains(uuid)).map(uuid -> {
            //如果id不包含在已发送，表t_common_message_status插入新数据
            CommonMessageStatusDto dto = new CommonMessageStatusDto();
            dto.setUuid(StringUtils.buildUUID());
            dto.setMessageUuid(uuid);
            dto.setTargetUuid(msgParam.getUserUuid());
            dto.setStatus(CommonConstant.MSG_STATUS_NOREAD);
            dto.setType(CommonMessageStatusDto.TYPE_SYSTEM);
            dto.setAppid(msgParam.getAppid());
            dto.setTargetType(CommonConstant.PASSENGER);
            return dto;
        }).collect(Collectors.toList());

        if (messageStatusDtoList.size() > 0) {
            commonMessageStatusService.addBatch(messageStatusDtoList);
        }
        return ResponseData.buildSuccessResponse(passengerMessageMapper.queryPasMsg(paramMap, new PageBounds(msgParam.getNowPage(),
                msgParam.getPageSize())));
    }

    @Override
    public ResponseData<Boolean> add(PassengerMessageDto dto) {
        String linkUrl = dto.getLinkUrl();
        //不包含
        if (linkUrl.indexOf(HTTP) == -1 && linkUrl.indexOf(HTTPS) == -1) {
            linkUrl = "http://" + linkUrl;
            dto.setLinkUrl(linkUrl);
        }
        return ResponseData.buildSuccessResponse(passengerMessageMapper.add(dto) > 0);
    }
}
